﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using zhangmenren.Models;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.IService;
using Newtonsoft.Json;

namespace zhangmenren.Areas.Admin.Controllers
{
    [Area(areaName: "Admin")]
    public class EnterpriseController :ABaseController
    {
        private IEnterpriseService enterpriseService;
        public EnterpriseController(IEnterpriseService _enterpriseService)
        {
            enterpriseService = _enterpriseService;
        }

        //名企名录控制器
        public IActionResult Index()
        {
            return View();
        }


        public async Task<string> GetEnterprise(int page = 1)
        {
            int total = 0;
            IEnumerable<EnterpriseEntity> dataList = null;
            dataList = await enterpriseService.Select();
            if (dataList.Any())
            {
                total = dataList.Count();
            }
            return JsonConvert.SerializeObject(new {code="0",msg="查询成功", data = dataList, count = total });

        }


        public IActionResult Add()
        {
            return View();
        }


        [HttpPost]
        public JsonResult Add(EnterpriseEntity entity)
        {
            entity.State = 1;
            if (entity != null)
            {
                var result = enterpriseService.Add(entity);
                return Json(new { state = "ok", msg = "添加成功" });
            }
            return Json(new { state = "fail", msg = "添加错误，请稍后重试。" });
        }


        [HttpPost]
        public JsonResult Del(int id = 0)
        {
            if (id != 0)
            {
                var result = enterpriseService.Del(id);
                return Json(new { state = "ok", msg = "删除成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }


        [HttpPost]
        public JsonResult EditSort(int num,int id = 0)
        {
            if (id != 0)
            {
                var result = enterpriseService.EditSort(id,num);
                return Json(new { state = "ok", msg = "修改成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }

        [HttpPost]
        public JsonResult Tongyi(int id = 0)
        {
            if (id != 0)
            {
                var result = enterpriseService.EditState(id);
                return Json(new { state = "ok", msg = "修改成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }
    }
}
